package com.bawei.mall.product.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bawei.mall.product.domain.MallProductCarInfo;
import com.bawei.mall.product.domain.detail.SkuStock;
import com.bawei.mall.product.domain.reponse.MallProductCarInfoResponse;
import com.bawei.mall.product.mapper.MallProductCarInfoMapper;
import com.bawei.mall.product.service.IMallProductCarInfoService;
import com.bawei.system.domain.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author：韩丹阳
 * @Package：com.bawei.mall.product.service.impl
 * @Project：mall_cloud
 * @name：IMallProductCarInfoServiceImpl
 * @Date：2024/7/5 下午 03:32
 */
@Service
public class IMallProductCarInfoServiceImpl extends ServiceImpl<MallProductCarInfoMapper, MallProductCarInfo> implements IMallProductCarInfoService {

    @Autowired
    private MallProductCarInfoMapper mallProductCarInfoMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private MallProductInfoServiceImpl mallProductInfoService;

    @Override
    public List<MallProductCarInfoResponse> findCarList() {
        User user = getInfo();
        ArrayList<MallProductCarInfoResponse> mallProductCarInfoResponses1 = new ArrayList<>();
        List<MallProductCarInfoResponse> mallProductCarInfoResponses = mallProductCarInfoMapper.findCarList(user.getId());
        List<SkuStock> skuStockList = mallProductInfoService.getSkuStockList();
        for (MallProductCarInfoResponse mallProductCarInfoRespons : mallProductCarInfoResponses) {
            for (SkuStock skuStock : skuStockList) {
                if (skuStock.getId().equals(mallProductCarInfoRespons.getProductSkuId())){
                    mallProductCarInfoRespons.setProductAttr(skuStock.getSpData());
                }
            }
            mallProductCarInfoResponses1.add(mallProductCarInfoRespons);
        }
        return mallProductCarInfoResponses1;
    }

    @Override
    public void add(MallProductCarInfo mallProductCarInfo) {
        User user = getInfo();
        mallProductCarInfo.setUserId(user.getId());
//        mallProductCarInfoMapper.add(mallProductCarInfo);
        mallProductCarInfoMapper.insert(mallProductCarInfo);
    }

    @Override
    public void clear() {
//        User user = getInfo();
//        mallProductCarInfoMapper.clear(user.getId());
        User user = getInfo();
        Map<String, Object> map = new HashMap<>();
        map.put("user_id",user.getId());
        this.mallProductCarInfoMapper.deleteByMap(map);
    }

    @Override
    public void delete(Integer ids) {
        User user = getInfo();
//        mallProductCarInfoMapper.deleteIds(ids,user.getId());
        mallProductCarInfoMapper.delete(Wrappers.<MallProductCarInfo>lambdaQuery()
                .eq(MallProductCarInfo::getId,ids)
                .eq(MallProductCarInfo::getUserId,user.getId()));
    }

    @Override
    public void updQuantity(Integer id, Integer quantity) {
//        mallProductCarInfoMapper.updQuantity(id,quantity);

        UpdateWrapper<MallProductCarInfo> mallProductCarInfoUpdateWrapper = new UpdateWrapper<>();
        mallProductCarInfoUpdateWrapper.eq("id",id);
        mallProductCarInfoUpdateWrapper.set("sum",quantity);
        mallProductCarInfoMapper.update(null,mallProductCarInfoUpdateWrapper);

    }



    /**
     * 获取用户信息
     */
    public User getInfo() {
        String token = redisTemplate.opsForValue().get("token");
        String cusUserJSONString = redisTemplate.opsForValue().get(token);
        User user = JSONObject.parseObject(cusUserJSONString, User.class);
        return user;
    }

}
